package com.xx.pmis.web.actions;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.HotelExpense;
import com.xx.pmis.model.OtherExpense;
import com.xx.pmis.model.TourismPlan;
import com.xx.pmis.model.TravellingExpense;
import com.xx.pmis.model.util.TourismPlanMX;
import com.xx.pmis.services.HotelExpenseService;
import com.xx.pmis.services.OtherExpenseService;
import com.xx.pmis.services.TourismPlanService;
import com.xx.pmis.services.TravellingExpenseService;
import com.xx.pmis.web.forms.TourismPlanForm;
import com.xx.pmis.web.util.AccountUtil;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.Rand;
import com.xx.pmis.web.util.TourismBudget;

/**
 * @class 旅游计划Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class TourismPlanAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(TourismPlanAction.class);

	TourismPlanService tourismPlanService = getPMISServices().getTourismPlanService();
	TravellingExpenseService travellingExpenseService = getPMISServices().getTravellingExpenseService();
	HotelExpenseService hotelExpenseService = getPMISServices().getHotelExpenseService();
	OtherExpenseService otherExpenseService = getPMISServices().getOtherExpenseService();

	TourismBudget tourismBudget = new TourismBudget();
	AccountUtil accountUtil = new AccountUtil();

	// 查询所有旅游计划
	public ActionForward listTourismPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<TourismPlan> tourismPlanList = null;
		try {
			tourismPlanList = tourismPlanService.getAllTourismPlanByCondition(" order by beginDate desc");
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划】查询所有【旅游计划】异常！" + e.toString());
		}
		// 实时汇总该旅游计划的消费总额
		Iterator<?> it = tourismPlanList.iterator();
		while (it.hasNext()) {
			TourismPlan tourismPlan = (TourismPlan) it.next();
			String totalMoney = "";
			try {
				totalMoney = tourismBudget.sumOfTourismExpenses(tourismPlan);
			} catch (Exception e) {
				log.info("【旅游管理/旅游计划】计算【旅游计划】实际支出异常！" + e.toString());
			}
			tourismPlan.setTotalMoney(accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(Double
					.parseDouble(totalMoney))));
			tourismPlan.setExceedMoney(Double.parseDouble(new DecimalFormat("####.##").format(Double
					.parseDouble(totalMoney)
					- Double.parseDouble(tourismPlan.getEstimate()))));

		}
		request.setAttribute("position", "旅游管理/旅游计划");
		request.setAttribute("tourismPlanList", tourismPlanList);
		return mapping.findForward("listTourismPlan");
	}

	// 新增
	public ActionForward addTourismPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("date", CurrentDate.currentDate());
		request.setAttribute("position", "旅游计划/新增");
		return mapping.findForward("addTourismPlan");
	}

	// 新增，处理方法
	public ActionForward addTourismPlan_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		TourismPlanForm f = (TourismPlanForm) form;

		String tpName = f.getTpName(); // 计划名称
		String beginDate = f.getBeginDate(); // 开始时间
		String endDate = f.getEndDate(); // 结束时间
		String tpDays = f.getTpDays(); // 计划天数
		String estimate = f.getEstimate(); // 估计费用
		String tpDesc = f.getTpDesc(); // 描述
		String tpType = f.getTpType(); // 0 旅游;1 出差;2 探亲

		TourismPlan tourismPlan = new TourismPlan();
		tourismPlan.setTpID("80" + Rand.getRand());
		tourismPlan.setTpName(tpName);
		tourismPlan.setBeginDate(beginDate);
		tourismPlan.setEndDate(endDate);
		tourismPlan.setTpDays(tpDays);
		tourismPlan.setEstimate(accountUtil.if1ZeroTo2Zero(estimate));
		tourismPlan.setTpState("1");
		tourismPlan.setTpDesc(tpDesc);
		tourismPlan.setTpType(tpType);
		try {
			tourismPlanService.saveOrUpdateTourismPlan(tourismPlan);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/新增】保存或更新【旅游计划】异常！" + e.toString());
		}

		listTourismPlanByPage(mapping, form, request, response);
		return mapping.findForward("listTourismPlan");
	}

	// 编辑
	public ActionForward editTourismPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String tpID = request.getParameter("tpID");
		String pageNum = request.getParameter("pageNum");

		TourismPlan tourismPlan = new TourismPlan();
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(tpID);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/编辑】根据【旅游计划ID】查询该【旅游计划】异常！" + e.toString());
		}

		request.setAttribute("pageNum", pageNum);
		request.setAttribute("tourismPlan", tourismPlan);
		request.setAttribute("position", "旅游计划/编辑");
		return mapping.findForward("editTourismPlan");
	}

	// 编辑，处理方法
	public ActionForward editTourismPlan_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");

		TourismPlanForm f = (TourismPlanForm) form;
		String tpID = f.getTpID(); // 计划ID
		String tpName = f.getTpName(); // 计划名称
		String beginDate = f.getBeginDate(); // 开始时间
		String endDate = f.getEndDate(); // 结束时间
		String tpDays = f.getTpDays(); // 计划天数
		String estimate = f.getEstimate(); // 估计费用
		String tpDesc = f.getTpDesc(); // 描述
		String tpType = f.getTpType(); // 0 旅游;1 出差;2 探亲

		TourismPlan tourismPlan = null;
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(tpID);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/编辑】根据【旅游计划ID】查询该【旅游计划】异常！" + e.toString());
		}
		tourismPlan.setTpName(tpName);
		tourismPlan.setBeginDate(beginDate);
		tourismPlan.setEndDate(endDate);
		tourismPlan.setTpDays(tpDays);
		tourismPlan.setEstimate(estimate);
		tourismPlan.setTpState("1");
		tourismPlan.setTpDesc(tpDesc);
		tourismPlan.setTpType(tpType);
		try {
			tourismPlanService.saveOrUpdateTourismPlan(tourismPlan);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/编辑】保存或更新【旅游计划】异常！" + e.toString());
		}

		request.setAttribute("pageNum", pageNum);
		listTourismPlanByPage(mapping, form, request, response);
		return mapping.findForward("listTourismPlan");
	}

	// 删除
	public ActionForward delTourismPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");

		TourismPlan tourismPlan = null;
		TourismPlanForm f = (TourismPlanForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(ids[i]);
			} catch (Exception e) {
				log.info("【旅游管理/旅游计划/删除】根据【旅游计划ID】查询该【旅游计划】异常！" + e.toString());
			}
			try {
				tourismPlanService.deleteTourismPlan(tourismPlan);
			} catch (Exception e) {
				log.info("【旅游管理/旅游计划/删除】删除【旅游计划】异常！" + e.toString());
			}
		}

		request.setAttribute("pageNum", pageNum);
		listTourismPlanByPage(mapping, form, request, response);
		return mapping.findForward("listTourismPlan");
	}

	// 完成计划
	public ActionForward accomplishTourismPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String tpID = request.getParameter("tpID");
		String pageNum = request.getParameter("pageNum");

		TourismPlan tourismPlan = null;
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(tpID);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/完成】根据【旅游计划ID】查询该【旅游计划】异常！" + e.toString());
		}
		tourismPlan.setTpState("0");

		try {
			tourismPlanService.saveOrUpdateTourismPlan(tourismPlan);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/完成】保存或更新【旅游计划】异常！" + e.toString());
		}

		request.setAttribute("pageNum", pageNum);
		listTourismPlanByPage(mapping, form, request, response);
		return mapping.findForward("listTourismPlan");
	}

	// 分页显示
	public ActionForward listTourismPlanByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = tourismPlanService.getCountOfTourismPlanByCondition("");
		} catch (Exception e) {
			String errorPage = "【旅游管理/交通费】查询【交通费】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}
		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<?> all = null;
		try {
			all = tourismPlanService.getAllTourismPlanByConditionByPage(" order by beginDate desc", Integer
					.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划】查询所有【旅游计划】异常！" + e.toString());
		}
		// 实时汇总该旅游计划的消费总额
		Iterator<?> it = all.iterator();
		while (it.hasNext()) {
			TourismPlan tourismPlan = (TourismPlan) it.next();
			String totalMoney = "";
			try {
				totalMoney = tourismBudget.sumOfTourismExpenses(tourismPlan);
			} catch (Exception e) {
				log.info("【旅游管理/旅游计划】计算【旅游计划】实际支出异常！" + e.toString());
			}
			log.info(tourismPlan.getTpName() + "********************************************************************************" + totalMoney
					+ "%%%");

			tourismPlan.setTotalMoney(accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(Double
					.parseDouble(totalMoney))));
			tourismPlan.setExceedMoney(Double.parseDouble(new DecimalFormat("####.##").format(Double
					.parseDouble(totalMoney)
					- Double.parseDouble(tourismPlan.getEstimate()))));

		}
		request.setAttribute("tourismPlanList", all);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}
		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "tourismPlan.do");
		request.setAttribute("position", "旅游管理/交通费");
		return mapping.findForward("listTourismPlan");
	}

	// 明细
	public ActionForward detailTourismPlan(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String tpID = request.getParameter("tpID");
		TourismPlan tourismPlan = null;
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(tpID);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/明细】根据【旅游计划ID】查询该【旅游计划】异常！" + e.toString());
		}
		// 计划明细列表
		List<TourismPlanMX> list = new ArrayList<TourismPlanMX>();
		TourismPlanMX tourismPlanMX = null;
		// 交通费
		try {
			List<TravellingExpense> teList = travellingExpenseService
					.getAllTravellingExpenseByCondition(" where tpID = '" + tpID + "'");
			double teMoney = 0.0;
			for (TravellingExpense te : teList) {
				TourismPlanMX tpMX = new TourismPlanMX();
				tpMX.setDate(te.getTeDate());
				tpMX.setName(te.getTeType());
				tpMX.setSubject("交通费");
				tpMX.setMoney(accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(Double.parseDouble(te
						.getTeExpenses())
						* Double.parseDouble(te.getTeNumber()))));
				tpMX.setDescription(te.getTeDesc());

				teMoney = teMoney + Double.parseDouble(te.getTeExpenses()) * Double.parseDouble(te.getTeNumber());

				list.add(tpMX);
			}
			tourismPlanMX = new TourismPlanMX();
			tourismPlanMX.setDate("");
			tourismPlanMX.setName("合计");
			tourismPlanMX.setSubject("交通费");
			tourismPlanMX.setMoney(accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(teMoney)));
			tourismPlanMX.setDescription("");
			list.add(tourismPlanMX);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/明细】根据【旅游计划ID】查询【交通费】异常！" + e.toString());
		}
		// 住宿费
		try {
			List<HotelExpense> heList = hotelExpenseService.getAllHotelExpenseByCondition(" where tpID = '" + tpID
					+ "'");
			double heMoney = 0.0;
			for (HotelExpense he : heList) {
				TourismPlanMX tpMX = new TourismPlanMX();
				tpMX.setDate(he.getTargetDate());
				tpMX.setName(he.getHeName());
				tpMX.setSubject("住宿费");
				tpMX.setMoney(accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(Double.parseDouble(he
						.getHeExpenses())
						* Double.parseDouble(he.getDayNumber()))));
				tpMX.setDescription(he.getHeDesc());
				heMoney = heMoney + Double.parseDouble(he.getHeExpenses()) * Double.parseDouble(he.getDayNumber());

				list.add(tpMX);
			}
			tourismPlanMX = new TourismPlanMX();
			tourismPlanMX.setDate("");
			tourismPlanMX.setName("合计");
			tourismPlanMX.setSubject("住宿费");
			tourismPlanMX.setMoney(accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(heMoney)));
			tourismPlanMX.setDescription("");
			list.add(tourismPlanMX);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/明细】根据【旅游计划ID】查询【交通费】异常！" + e.toString());
		}
		// 其他费用
		try {
			List<OtherExpense> oeList = otherExpenseService.getAllOtherExpenseByCondition(" where tpID = '" + tpID
					+ "'");
			double oeMoney = 0.0;
			for (OtherExpense oe : oeList) {
				TourismPlanMX tpMX = new TourismPlanMX();
				tpMX.setDate(oe.getOeDate());
				tpMX.setName(oe.getOeType());
				tpMX.setSubject("其他");
				tpMX.setMoney(accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(Double.parseDouble(oe
						.getOeExpenses()))));
				tpMX.setDescription(oe.getOeDesc());
				oeMoney = oeMoney + Double.parseDouble(oe.getOeExpenses());

				list.add(tpMX);
			}
			tourismPlanMX = new TourismPlanMX();
			tourismPlanMX.setDate("");
			tourismPlanMX.setName("合计");
			tourismPlanMX.setSubject("其他");
			tourismPlanMX.setMoney(accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(oeMoney)));
			tourismPlanMX.setDescription("");
			list.add(tourismPlanMX);
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划/明细】根据【旅游计划ID】查询【交通费】异常！" + e.toString());
		}
		request.setAttribute("list", list);
		request.setAttribute("tourismPlan", tourismPlan);
		request.setAttribute("position", "旅游计划/明细");
		return mapping.findForward("detailTourismPlan");
	}

	// 数据分析，查询所有旅游计划
	public ActionForward listTourismPlanByYear(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<TourismPlan> tourismPlanList = null;
		// 数据分析
		try {
			tourismPlanList = tourismBudget.getTourismPlanList();
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划】查询所有【旅游计划】异常！" + e.toString());
		}

		String year = CurrentDate.currentYear();
		int yearInt = Integer.parseInt(year);
		request.setAttribute("year1", year);
		request.setAttribute("year2", String.valueOf(yearInt - 1));
		request.setAttribute("year3", String.valueOf(yearInt - 2));
		request.setAttribute("year4", String.valueOf(yearInt - 3));
		request.setAttribute("year5", String.valueOf(yearInt - 4));

		request.setAttribute("position", "旅游管理/旅游计划");
		request.setAttribute("tourismPlanList", tourismPlanList);
		return mapping.findForward("listTourismPlanByType");
	}
}